在SQL Server Management Studio中使用SQL语句创建数据表并添加相关约束 |
您所在的位置:网站首页 › sql server数据库文件名后缀 › 在SQL Server Management Studio中使用SQL语句创建数据表并添加相关约束 |
SQL Server中使用SQL语句创建数据表并添加约束的基础教程
1. 创建数据库 创建数据表之前,需要先创建用户数据库(不推荐在系统数据库中创建表格),再去自己创建的数据库中创建相应的数据表,创建数据库的SQL语句如下(Ctrl+N 新建查询写SQL语句): create database StudentMIS--创建一个名为StudentMIS的数据库选中并执行该语句,左侧的对象资源管理器中出现名为StudentMIS的数据库后表示创建成功(如果执行语句没有报错,但并没有出现数据库的,可以在对象资源管理器中刷新一下数据库),如图所示: 2. 创建数据表 有了数据库之后便可以开始写SQL语句创建表格,但在此之前,应调整目前正在操作的数据库,否则创建出来的表格是在系统数据库中(可以在查询界面的底部查看当前正在操作的数据库),如图: 以上语句可以创建出一个如图的表: 错误如下: 消息 2749,级别 16,状态 2,第 1 行 标识列 'Number' 的数据类型必须是 int、bigint、smallint、tinyint 或 decimal,或者是小数位数为 0 的 numeric 数据类型,并且约束为不可为 Null。3. 添加约束 表格创建成功后,为了确保数据的完整性,我们还需要为表格添加一系列的约束,比如主键、唯一约束、检查约束、默认值、外键等。现在咱们就一一进行讲解。 主键约束:设置主键约束有两种方式,可以在创建表的同时设置主键,也可以在表格创建完成之后在单独添加。 在建表的同时设置主键(以Number字段举例): create table StudentInfo( ID int not null identity(1,1),--编号 Number char(10) not null primary key,--学号 主键 Name varchar(20) not null,--姓名 Sex char(2) null,--性别 Age int,--年龄 Birthday date,--出生日期 Address varchar(50),--家庭住址 Class varchar(10)--班级 )表格创建之后单独添加约束: alter table StudentInfo add constraint PK_StudentInfo_Number primary key (Number)这两种方式均可。 如果需要添加多个列作为主键列,则可以使用以下语法: alter table 表名 add constraint 约束名称 约束 (字段1,字段2,...) 例如:alter table StudentInfo add constraint PK_StudentInfo_Number primary key (ID,Number,Name)唯一约束:同主键约束一样,唯一约束也可以有两种添加方式。 在建表的时候设置(以ID字段示例): create table StudentInfo( ID int not null identity(1,1) unique,--编号 唯一键 Number char(10) not null primary key,--学号 主键 Name varchar(20) not null,--姓名 Sex char(2) null,--性别 Age int,--年龄 Birthday date,--出生日期 Address varchar(50),--家庭住址 Class varchar(10)--班级 )表格创建之后单独添加约束: alter table StudentInfo add constraint UQ_StudentInfo_ID unique (ID)默认值约束: 在建表的时候设置(以Sex字段示例): create table StudentInfo( ID int not null identity(1,1) unique,--编号 唯一键 Number char(10) not null primary key,--学号 主键 Name varchar(20) not null,--姓名 Sex char(2) null default '男',--性别 默认为男 Age int,--年龄 Birthday date,--出生日期 Address varchar(50),--家庭住址 Class varchar(10)--班级 )表格创建之后单独添加约束: alter table StudentInfo add constraint DF_StudentInfo_Sex default '男' for Sexcheck约束(检查约束): 在建表的时候设置(以Sex字段示例): create table StudentInfo( ID int not null identity(1,1) unique,--编号 唯一键 Number char(10) not null primary key,--学号 主键 Name varchar(20) not null,--姓名 Sex char(2) null default '男' check (Sex='男' or Sex='女'),--性别 默认为男,限制为男或女 Age int,--年龄 Birthday date,--出生日期 Address varchar(50),--家庭住址 Class varchar(10)--班级 )表格创建之后单独添加约束: alter table StudentInfo add constraint CK_StudentInfo_Sex check (Sex='男' or Sex='女')外键约束:添加外键约束需要两个表,所以我们使用如下语句再创建一个分数表: create table Score( Number char(10) not null ,--学号 Subject varchar(20) not null,--科目 Score decimal(4,1)--分数 )在Score表中创建外键与StudentInfo表建立关系… 在创建表的同时设置外键: create table Score( Number char(10) not null foreign key references StudentInfo(Number) ,--学号 Subject varchar(20) not null,--科目 Score decimal(4,1)--分数 )表格创建之后单独添加约束: alter table Score add constraint FK_Score_StudentInfo_Number foreign key (Number) references StudentInfo(Number)注意:在添加外键约束时,主键表中相应的字段必须为主键或者是唯一键,否则就会出现以下的错误: 消息 1776,级别 16,状态 0,第 1 行 在被引用表 'StudentInfo' 中没有与外键 'FK_Score_StudentInfo_Number' 中的引用列列表匹配的主键 或候选键。 消息 1750,级别 16,状态 0,第 1 行 无法创建约束。请参阅前面的错误消息。完整代码如下: create database StudentMIS; use StudentMIS create table StudentInfo( ID int not null identity(1,1) unique,--编号 唯一键 Number char(10) not null primary key,--学号 主键 Name varchar(20) not null,--姓名 Sex char(2) null default '男' check (Sex='男' or Sex='女'),--性别 默认为男,限制为男或女 Age int,--年龄 Birthday date,--出生日期 Address varchar(50),--家庭住址 Class varchar(10)--班级 ) alter table StudentInfo add constraint PK_StudentInfo_Number primary key (Number) alter table StudentInfo add constraint PK_StudentInfo_Number primary key (ID,Number,Name) alter table StudentInfo add constraint UQ_StudentInfo_ID unique (ID) alter table StudentInfo add constraint DF_StudentInfo_Sex default '男' for Sex alter table StudentInfo add constraint CK_StudentInfo_Sex check (Sex='男' or Sex='女') create table Score( Number char(10) not null foreign key references StudentInfo(Number) ,--学号 Subject varchar(20) not null,--科目 Score decimal(4,1)--分数 ) alter table Score add constraint FK_Score_StudentInfo_Number foreign key (Number) references StudentInfo(Number) |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |